<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>卡管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript" src="${ctxStatic}/common/base-loading.js"></script>
	<style type="text/css">
	.container{
		width:380px;
	}
	div.container {
		background-color: #eee;
		border: 1px solid red;
		margin: 5px;
		padding: 5px;
	}
	div.container ol li {
		list-style-type: disc;
		margin-left: 20px;
	}
	div.container {
		display: none
	}
	.container label.error {
		display: inline;
	}
		.cmxform fieldset p.error label {
		color: red;
	}
	</style>
	<script type="text/javascript">
	$(document).ready(function() {
		setTimeout(GetDate, 1000);
		var container = $('div.container');
		$("#distributionForm").validate({
			errorContainer: container,
			errorLabelContainer: $("ol", container),
			wrapper: 'li',
			rules:{
				mainPriceValue:{ 
                	checkMainPrice:true,
                },
                videPriceValue:{ 
                	checkVidePricePrice:true,
                }
            },
			submitHandler: function(form){
				loading('正在提交，请稍等...');
				form.submit();
			},
		});
		 $.validator.addMethod("checkMainPrice",function(value,element,params){ 
	            var price = $("#tcPrice").val();
	            return this.optional(element)||(parseInt(price) <= parseInt(value));  
	        },"主套餐销售价格不能小于成本价格！");
		 
		 $.validator.addMethod("checkVidePricePrice",function(value,element,params){ 
	            var price = $("#videTcPrice").val();
	            return this.optional(element)||(parseInt(price) <= parseInt(value));  
	        },"叠加套餐销售价格不能小于成本价格！");
		 
			$("#checkAll").click(function () {
				$("input[name='id']:checkbox").prop("checked", this.checked);            
			});

		});  
		function page(n,s){
			$("#pageNo").val(n);
			$("#pageSize").val(s);
			$("#searchForm").attr("action","${ctx}/card/card/again_list");
			$("#searchForm").submit();
        	return false;
        }
		//清空
		function cleanForm(){
			 $(':input','#searchForm')
		       .not(':button,:submit,:reset,:hidden')
		       .val('')
		       .removeAttr('checked');
			 $(".select2-chosen").text("");
		       $.jBox.tip('清空成功。', 'success');
		}
		//批量分配
		var batchList = [];
		function batchDistribution(){
			//套餐列表
			var delList = [];
			$("#contentTable .checkbox-hook:checked").each(function (){
				delList.push($(this).val());
			})
			if(delList.length == 0){
				$.jBox.tip('请至少选择一条数据进行分配。', 'error');
			}else{
				//查询套餐是否一致
				$.ajax({
					url : "${ctx}/card/card/isKindCard?id="+delList,
					type : "GET",
					cache:true,
					beforeSend: function () {
						loading('数据校验中...');
					},
					complete: function () {
						closeTip();
					},
					success : function(data) {
						if(data.code == '0'){
							$("#taocanName").val(data.taocanName);
							$("#taocanType").val(data.taocanType);
							$("#tcPrice").val(data.tcPrice);
							$("#videTaocanName").val(data.videTaocanName);
							$("#videTcPrice").val(data.videTcPrice);
							batchList = delList;
							$("#distributionIds").val(delList);//添加需要分配的id
							$('#myModal').modal("show");//打开模态框
						}else{
							$.jBox.tip(data.taocanName, 'error');
							return false;
						}
					}
				});
			}
		} 

		function GetDate(){
			$("#tbData tbody").find("tr").each(function () {
			 var statusName = "";
             var cardType = $(this).find("td:eq(9)");//状态
             var prodname = $(this).find("td:eq(10)");//资费计划
             var total = $(this).find("td:eq(11)");//总量
             var used = $(this).find("td:eq(12)");//用量
             var left = $(this).find("td:eq(13)");//剩余
             var iccid = $(this).find("td:eq(8)").html();//iccid
             var areaId = $(this).find("td:eq(2)").html();//所属地区
             //查询卡状态
              $.ajax({
                type: "get",
                async: true,
                url: "${ctx}/card/card/selectCardInfo?iccid=" + iccid+"&areaId="+areaId+"&ebid="+"0001000000009",
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                success: function (data, textStatus) {
					if(data.code == "0000"){
						var resObjs1 = jQuery.parseJSON(data.body);
						var result = resObjs1.result[0];
		    			switch (result.STATUS) {
						case '00':
							statusName = '正常';
							break;
						case '01':
							statusName = '单向停机';
							break;
						case '02':
							statusName = '停机';
							break;
						case '03':
							statusName = '预销号';
							break;
						case '04':
							statusName = '销号';
							break;
						case '05':
							statusName = '过户';
							break;
						case '06':
							statusName = '休眠';
							break;
						case '07':
							statusName = '待激';
							break;
						case '99':
							statusName = '号码不存在';
							break;
						default:
							break;
					}
		    			cardType.html(statusName);
					}else{
						cardType.html("<span style='color:red;'>查询失败</span>");
					}
                    
                }
            }); 
             //查询卡的流量
              $.ajax({
                  type: "get",
                  async: true,
                  url: "${ctx}/card/card/selectCardInfo?iccid=" + iccid+"&areaId="+areaId+"&ebid="+"0001000000083",
                  contentType: "application/x-www-form-urlencoded; charset=utf-8",
                  success: function (data, textStatus) {
  					if(data.code == "0000"){
  						var resObjs = jQuery.parseJSON(data.body);
  						var gprsObj2 = resObjs.result[0].gprs[0];
  						prodname.html(gprsObj2.prodname+"_"+gprsObj2.total);
  						total.html(gprsObj2.total);
  						left.html(gprsObj2.left);
  						used.html(gprsObj2.used);
  					}else{
  						prodname.html("<span style='color:red;'>查询失败</span>");
  						total.html("<span style='color:red;'>查询失败</span>");
  						left.html("<span style='color:red;'>查询失败</span>");
  						used.html("<span style='color:red;'>查询失败</span>"); 
  					}
                      
                  }
              }); 
        })
	}
	</script>
</head>
<body>
	<!-- 分配DIV -->
	<div id="myModal" style="width:400px;" class="modal hide fade" tabindex="-1" role="dialog"  aria-hidden="true">
		<form id="distributionForm" method="post" action="${ctx}/card/card/againDistribution">
		  <div class="modal-header">
		    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		    <h3 id="myModalLabel">流量卡分配</h3>
		  </div>
		  <!-- 错误信息 -->
		  <div class="container">
			<h4>在你提交表单时出现了以下错误，详情如下：</h4>
			<ol>
			</ol>
		</div>
		  <div class="modal-body">
	  			<input type="hidden" id="distributionIds" name="ids" value=""/><!-- 主键id -->
				<input type="hidden" id="taocanType" name="taocanType" value=""/><!-- 主套餐类型 -->
	  		<div class="controls">
	    		<label class="control-label">所属代理商：</label>
	    		<sys:treeselect  id="company" name="company" value="" labelName="company.name" labelValue="" title="代理商" url="/sys/office/treeData?type=1"/>
	    		<span class="help-inline"><font color="red">*</font> </span>
	    	</div>
	    	<div class="controls" style="margin-top: 20px;">
		    	<label class="control-label">主套餐类型：</label>
		    	<input class="input-xlarge" id="taocanName" name="taocanName" style="border: 1px solid #ccc;padding: 4px 6px;width: 240px;" value="" readonly="readonly">
	    	</div>
	    	<div class="controls" style="margin-top: 20px;">
		    	<label class="control-label">成本价格：&nbsp;&nbsp;&nbsp;</label>
		    	<input class="input-xlarge" id="tcPrice" name="tcPrice" style="border: 1px solid #ccc;padding: 4px 6px;width: 80px;" value="" readonly="readonly">&nbsp;&nbsp;
		    	<label class="control-label">销售价格：&nbsp;</label>
		    	<input style="width: 80px;" id="mainPriceValue" name="mainPriceValue" htmlEscape="false" maxlength="8" class="input-xlarge number required"/>
		    	<span class="help-inline"><font color="red">*</font> </span>
	    	</div>
	    	<!-- 副套餐 -->
			<div class="controls" style="margin-top: 20px;">
		    	<label class="control-label">叠加包：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
		    	<input class="input-xlarge" id="videTaocanName" name="videTaocanName" style="border: 1px solid #ccc;padding: 4px 6px;width: 240px;" value="" readonly="readonly">
	    	</div>
	    	<div class="controls" style="margin-top: 20px;">
		    	<label class="control-label">成本价格：&nbsp;&nbsp;&nbsp;</label>
		    	<input class="input-xlarge" id="videTcPrice" name="videTcPrice" style="border: 1px solid #ccc;padding: 4px 6px;width: 80px;" value="" readonly="readonly">&nbsp;&nbsp;
		    	<label class="control-label">销售价格：&nbsp;</label>
		    	<input style="width: 80px;" id="videPriceValue" name="videPriceValue" htmlEscape="false" maxlength="8" class="input-xlarge number required"/>
		    	<span class="help-inline"><font color="red">*</font> </span>
	    	</div>
	  	</div>
	  <div class="modal-footer">
	    <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
	    <input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>
	  </div>
		</form>
	</div>

	<ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/card/card/again_list">卡列表</a></li>
		<shiro:hasPermission name="card:card:save"><li><a href="${ctx}/card/card/openSave">卡添加</a></li></shiro:hasPermission>
	</ul>
	<form:form id="searchForm" modelAttribute="card" action="${ctx}/card/card/again_list" method="post" class="breadcrumb form-search">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<ul class="ul-form">
			<li style="margin-left:20px;"><label>运营商分类：</label>
				<form:select path="operator" class="input-medium" style="width:177px;">
					<form:option value="" label=""/>
					<form:options items="${fns:getDictList('operator')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</li>
			<li style="margin-left:20px;"><label>品<span style="opacity:0;">啊啊啊</span>名：</label>
				<form:select path="brand" class="input-medium" style="width:177px;">
					<form:option value="" label=""/>
					<form:options items="${fns:getDictList('brand')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</li>
			<li style="margin-left:20px;"><label>套 餐 类 型：</label>
				<form:select path="pkg" class="input-medium" style="width:177px;">
					<form:option value="" label=""/>
					<form:options items="${fns:getDictList('pkg')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</li>
			<li style="margin-left:20px;"><label>卡<span style="opacity:0;">啊</span>类<span style="opacity:0;">啊</span>型：</label>
				<form:select path="cardType" class="input-medium" style="width:177px;">
					<form:option value="" label=""/>
					<form:options items="${fns:getDictList('cardType')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</li>
			<li style="margin-left:20px;"><label>编<span style="opacity:0;">啊啊啊</span>号：</label>
				<form:input path="phonenumber" htmlEscape="false" maxlength="20" class="input-medium"/>
			</li>
<%-- 			<li style="margin-left:20px;"><label>代<span style="opacity:0;">啊</span>理<span style="opacity:0;">啊</span>商：</label>
				<form:input path="office.id" htmlEscape="false" maxlength="20" class="input-medium"/>
			</li> --%>
			<li style="margin-left:20px;"><label>ICCID&nbsp;&nbsp;(始)：</label>
				<form:input path="iccidStart" htmlEscape="false" maxlength="30" class="input-medium"/>
			</li>
			<li style="margin-left:20px;"><label>ICCID&nbsp;&nbsp;(止)：</label>
				<form:input path="iccidEnd" htmlEscape="false" maxlength="30" class="input-medium"/>
			</li>
			<li style="margin-left:20px !important;" class="btns">
				<a id="btnSubmit" class="btn btn-primary"  onclick="return page();"><i class="icon-search"></i>&nbsp;查询</a>
			</li>
			<li style="margin-left:20px !important;" class="btns">
				<a id="btnSubmit" class="btn btn-warning" onclick="cleanForm();"><i class="icon-refresh"></i>&nbsp;清空</a>
			</li>
		</ul>
	</form:form>
	<div class="breadcrumb">
	  	<shiro:hasPermission name="card:card:againDistribution">
		  		<a type="button" onclick="batchDistribution()" class="btn btn-primary" ><i class="icon-pencil"></i>&nbsp;批量重新分配</a>
	  	</shiro:hasPermission>
	</div>
	<sys:message content="${message}"/>
	<table id="contentTable"  class="table  table-striped table-bordered table-condensed table-hover table-responsive" >
		<thead>
			<tr>
				<th><input type="checkbox" id="checkAll"/></th>
				<th>所属运营商</th>
				<th>所属地区id</th>
				<th>品名</th>
				<th>套餐类型</th>
				<th>卡类型</th>
				<th>所属代理商</th>
				<th>编号</th>
				<th>ICCID</th>
				<th>卡状态</th>
				<th>资费计划</th>
				<th>流量总值</th>
				<th>本月用量</th>
				<th>剩余总值</th>
				<th>流量预警阈值</th>
				<th>最终所属用户</th>
				<c:if test="${officeId == 1 }">
					<th>所属地区</th>
				</c:if>
			</tr>
		</thead>
		<tbody>
		<c:choose>
			<c:when test="${not empty page.list}">
				<c:forEach items="${page.list}" var="card">
					<tr>
						<td><input class="checkbox-hook" type="checkbox" name="id" value="${card.id}"/></td>
						<td class="yidong">
							<c:if test="${card.operator == 1}">中国移动</c:if>
							<c:if test="${card.operator == 2}">中国电信</c:if>
							<c:if test="${card.operator == 3}">中国联通</c:if>
						</td>
						<td>
							${card.area.id}
						</td>
						<td>
							<c:if test="${card.brand == 1}">全球通</c:if>
							<c:if test="${card.brand == 2}">神州行</c:if>
							<c:if test="${card.brand == 3}">动感地带</c:if>
						</td>
						<td>
							${fns:getDictLabel(card.pkg, 'pkg', '未知')}
						</td>
						<td>
							<c:if test="${card.cardType == 1}">标准</c:if>
							<c:if test="${card.cardType == 2}">Micro</c:if>
							<c:if test="${card.cardType == 3}">Nano</c:if>
						</td>
						<td>
							<c:if test="${card.dbName != null}">${card.dbName}</c:if>
							<c:if test="${card.dbName == null}">${officeName}</c:if>
						</td>
						<td>
							${card.phonenumber}
						</td>
						<td>
							${card.iccid}
						</td>
						<td>正常</td>
						<td>
							<span >加载中...</span>
						</td>
						<td>
							<span >加载中...</span>
						</td>
						<td>
							<span >加载中...</span>
						</td>
						<td>
							<span >加载中...</span>
						</td>
						<td>
							${card.warning}
						</td>
						<td>
							${card.userName}
						</td>
						<c:if test="${officeId == 1 }">
							<td>
								${fns:getDictLabel(card.area.id, 'areaId', '未查询到')}
							</td>
						</c:if>
					</tr>
				</c:forEach>
			</c:when>
			<c:otherwise>
				<tr>
					<td colspan="16" style="text-align: center;font-size:17px; font-family:SimHei">未查询到数据...</td>
				</tr>
			</c:otherwise>
			</c:choose>
		</tbody>
	</table>
	<div class="pagination">${page}</div>
</body>
</html>